package com.citasgoo.common.utils;



import com.citasgoo.common.emus.EnumInfo;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

public class CodeUtils {

    private static String eMPTY = "";

    public static boolean hasCode(String blockCodes, String... searchCodes) {
        if (StringUtils.isEmpty(blockCodes)) {
            return false;
        }
        for (int i = 0; i < searchCodes.length; i++) {
            if (blockCodes.indexOf(searchCodes[i]) != -1) {
                return true;
            }
        }
        return false;
    }

    public static String unionBlockCodes(String blockCodes1, String blockCodes2) {
        if (blockCodes1 == null)
            blockCodes1 = "";
        if (blockCodes2 == null)
            blockCodes2 = "";

        char[] a = blockCodes1.toCharArray();
        char[] b = blockCodes2.toCharArray();
        Set<Character> set = new LinkedHashSet<Character>();
        for (char _a : a) {
            set.add(_a);
        }
        for (char _b : b) {
            set.add(_b);
        }
        StringBuilder result = new StringBuilder();
        Iterator<Character> iterator = set.iterator();
        while (iterator.hasNext()) {
            result.append(iterator.next());
        }

        if ("".equals(result)) {
            return null;
        } else {
            return result.toString();
        }
    }

    public static boolean hasBlockCode(String blockCode, String searchCodes) {
        return StringUtils.containsAny(blockCode, searchCodes);
    }

    public static boolean hasNoBlockCode(String blockCode, String searchCodes) {
        return !StringUtils.containsAny(blockCode, searchCodes);
    }

    public static String addBlockCode(String blockCode, String newCode) {
        if (StringUtils.isEmpty(blockCode)) {
            return newCode;
        }

        if (hasBlockCode(blockCode, newCode)) {
            return blockCode;
        }
        return new StringBuffer(blockCode).append(newCode).toString();
    }

    public static String removeBlockCode(String blockCode, String... removeCodes) {
        if (StringUtils.isEmpty(blockCode)) {
            return eMPTY;
        }
        for (String s : removeCodes) {
            blockCode = blockCode.replaceAll(s, eMPTY);
        }
        return blockCode;
    }

    public static String getEnumInfo(Enum<?> e) {
        String textValue = "";
        EnumInfo info = e.getClass().getAnnotation(EnumInfo.class);
        for (String strs : info.value()) {
            String[] strtemp = strs.split("\\|");
            if (strtemp[0].equals(e.name())) {
                textValue = strtemp[1];
                break;
            }
        }
        return textValue;
    }
}
